package com.matt.controller.web;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.domain.Children;
import com.matt.domain.ChildrenHealthBaseInfo;
import com.matt.foundation.constant.RequestConstant;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.commons.model.StatusResponse;
import com.matt.foundation.query.ChildrenQuery;
import com.matt.foundation.query.HealthQuery;
import com.matt.foundation.utils.DateUtil;
import com.matt.foundation.vo.ChildChildrenHealthListVo;
import com.matt.foundation.vo.ChildrenHealthListVo;
import com.matt.service.ChildrenHealthService;

import com.matt.service.ChildrenService;
import com.tuo51.foudation.utils.export.HealthReportUtil;
import io.swagger.annotations.ApiOperation;

import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.Interval;
import org.joda.time.PeriodType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/11/1
 * *******************************
 */
@RestController
@RequestMapping("/web/children_health")
public class WebChildrenHealthController {

    @Autowired
    private ChildrenHealthService childrenHealthService;

    @Autowired
    private MemVerificationCache cache;

    @Autowired
    private ChildrenService childrenService;

    @ApiOperation(value = "获取宝宝列表",httpMethod = "POST")
    @RequestMapping("/children_page")
    public StatusResponse page(ChildrenQuery query) {
        query.setActive(true);
        query.validateOrderByAndSequence();
        Page<ChildChildrenHealthListVo> childrenPage = childrenHealthService.getChildChildrenHealthPage(query);
        return StatusResponse.success(childrenPage);
    }

    @ApiOperation(value = "获取宝宝列表",httpMethod = "POST")
    @RequestMapping("/children_page2")
    public StatusResponse page2(ChildrenQuery query) {
        query.setActive(true);
        query.validateOrderByAndSequence();
        Page<ChildChildrenHealthListVo> childrenPage = childrenHealthService.getChildChildrenHealthPage(query);
        return StatusResponse.success(childrenPage);
    }

    @ApiOperation(value = "获取记录列表",httpMethod = "POST")
    @RequestMapping("/page")
    public StatusResponse page(HealthQuery query) {
        Page<ChildrenHealthListVo> healthPage = childrenHealthService.getChildrenHealthPage(query);
        return StatusResponse.success(healthPage);
    }

    @ApiOperation(value = "添加儿保评测",httpMethod = "POST")
    @RequestMapping("/add")
    public StatusResponse add(@RequestHeader(RequestConstant.TICKET) String ticket, ChildrenHealthBaseInfo childrenHealthBaseInfo, String childrenId, String height, String weight, String evaluateDate) {
        if (StringUtils.isNotBlank(evaluateDate))
            childrenHealthBaseInfo.setEvaluateTime(DateUtil.parseToDate(evaluateDate));
        childrenHealthBaseInfo.setTeacherId(cache.getStaffId(ticket));
        childrenHealthBaseInfo.setEvaluateName(cache.getStaff(ticket).getRealName());
        childrenHealthService.addHealthInfo(childrenHealthBaseInfo, childrenId, height, weight);
//        Children byId = childrenService.getById(childrenId);
//        byId.setCreated(new Date());
//        childrenService.updateById(byId);
        return StatusResponse.success();
    }

    @ApiOperation(value = "儿保详情",httpMethod = "POST")
    @RequestMapping("/detail")
    public StatusResponse page(@RequestHeader(RequestConstant.TICKET) String ticket, String id) {
        StaffDTO staff = cache.getStaff(ticket);
        Map healthMap = childrenHealthService.findHealthInfoDetail(staff, id);
        return StatusResponse.success(healthMap);
    }

    @ApiOperation(value = "修改儿保评测",httpMethod = "POST")
    @RequestMapping("/modify")
    public StatusResponse modify(@RequestHeader(RequestConstant.TICKET) String ticket, ChildrenHealthBaseInfo childrenHealthBaseInfo, String height, String weight) {
        childrenHealthBaseInfo.setTeacherId(cache.getStaffId(ticket));
        childrenHealthBaseInfo.setEvaluateName(cache.getStaff(ticket).getRealName());
        childrenHealthService.modifyHealthInfo(childrenHealthBaseInfo, height, weight);
        return StatusResponse.success();
    }

    @ApiOperation(value = "生长发育评价",httpMethod = "GET")
    @RequestMapping("/grow_evaluate_scale")
    public StatusResponse GrowEvaluateScale(@RequestHeader(RequestConstant.TICKET) String ticket,
                                            String childrenId,String height, String weight ) {

        Children children = childrenService.getById(childrenId);
        Map<String,String> resultMap = new HashMap<>();
        if(Objects.nonNull(children)){

            String g = children.getGender().intValue() == 0 ? "girl" : "boy";
            DateTime now = DateTime.now();
            Date birthday = children.getBirthday() == null ? now.toDate() : children.getBirthday();
            Interval interval = new Interval(birthday.getTime(),now.toDate().getTime());
            int month = interval.toPeriod(PeriodType.months()).getMonths();

            if(StringUtils.isNotBlank(weight)){
                Double w = Double.parseDouble(weight);
                String wResult = HealthReportUtil.calcWeightPercent(month,g,w);
                resultMap.put("wResult",wResult);
            }

            if(StringUtils.isNotBlank(height)){
                Double h = Double.parseDouble(height);
                String hResult = HealthReportUtil.calcHeightPercent(month,g,h);
                resultMap.put("hResult",hResult);
            }

        }

        return StatusResponse.success(resultMap);
    }
}
